<template>
  <el-dialog
      top="10vh"
      width="600px"
      title="添加/编辑"
      :visible.sync="dialogValue"
      :close-on-click-modal="false"
      append-to-body
  >
    <SubmitForm
        :rule-form="ruleForm"
        :rules="rules"
        :form-list="formList"
        @change="handleFormChange"
        @save="onSave"
        @close="dialogValue = false"
    />
  </el-dialog>
</template>

<script>
import {http} from '@/api'

export default {
  name: 'FormAdd',
  data() {
    return {
      apiKey: 'ticketShop',
      dialogValue: false,
      ruleForm: {},
      defaultForm: {
        name: '',
        cover: '',
        carousel: '',
        address: '',
        level: '',
        description: '',
        longitude: '',
        latitude: '',
        is_recommended: 0,
        status: 1
      },
      rules: {
        name: [{required: true, message: '请输入景点名称', trigger: 'blur'}],
        address: [{required: true, message: '请输入详细地址', trigger: 'blur'}],
      },
      formList: [
        {
          label: '景点名称',
          prop: 'name',
          name: 'input',
          colSpan: 12
        },
        {
          label: '级别',
          prop: 'level',
          name: 'input',
          colSpan: 12
        },
        {
          label: '营业开始时间',
          prop: 'open_start_time',
          name: 'time',
          colSpan: 12
        },
        {
          label: '营业结束时间',
          prop: 'open_end_time',
          name: 'time',
          colSpan: 12
        },
        {
          label: '封面图片',
          prop: 'cover',
          name: 'file',
          colSpan: 24
        },
        {
          label: '轮播图片',
          prop: 'carousel',
          name: 'file',
          multiple: true,
          colSpan: 24
        },
        {
          label: '描述',
          prop: 'description',
          name: 'input',
          type: 'textarea',
          colSpan: 24
        },
        {
          label: '经度',
          prop: 'longitude',
          name: 'input',
          colSpan: 12
        },
        {
          label: '纬度',
          prop: 'latitude',
          name: 'input',
          colSpan: 12
        },
        {
          label: '详细地址',
          prop: 'address',
          name: 'input',
          type: 'textarea',
          colSpan: 24
        },
        {
          label: '销售信息',
          name: 'title'
        },
        {
          // original_price
          label: '最低原价',
          prop: 'original_price',
          name: 'number',
          colSpan: 8
        },
        {
          // price
          label: '实际最低价',
          prop: 'price',
          name: 'number',
          colSpan: 8
        },
        {
          // virtual_sale_count
          label: '虚拟销量',
          prop: 'virtual_sale_count',
          name: 'number',
          colSpan: 8
        },
        {
          label: '是否推荐',
          prop: 'is_recommended',
          name: 'switch',
          colSpan: 24,
          activeValue: 1,
          inactiveValue: 0
        },
        {
          label: '状态',
          prop: 'status',
          name: 'switch',
          colSpan: 24,
          activeValue: 1,
          inactiveValue: 0
        },
        {
          label: '详情',
          name: 'rich',
          prop: 'content'
        },
        {
          label: '商户',
          name: 'title',
        },
        {
          label: '账号',
          name: 'input',
          prop: 'username'
        },
        {
          label: '密码',
          name: 'input',
          prop: 'password'
        },
      ]
    }
  },
  methods: {
    handleFormChange({key, val}) {
      this.ruleForm[key] = val
    },
    open(row) {
      this.dialogValue = true
      if (row) {
        http[this.apiKey].read(row.id).then(res => {
          this.ruleForm = {
            ...res.data,
          }
        })
      } else {
        this.ruleForm = {
          ...this.defaultForm
        }
      }
    },
    onSave() {
      http[this.apiKey][this.ruleForm.id ? 'update' : 'create'](this.ruleForm.id, {
        ...this.ruleForm,
      }).then(res => {
        this.$message.success('操作成功')
        this.dialogValue = false
        this.$emit('get-page')
      })
    }
  }
}
</script>

<style scoped lang="scss">
</style>
